package com.ruoyi.psi.purchase.order.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.purchase.apply.domain.PurchaseApplyList;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrder;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrderList;
import com.ruoyi.psi.purchase.order.service.IPurchaseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@RestController
@RequestMapping("/psi/purchase/order")
@Api(value = "采购订单模块", tags = "采购订单接口")
public class PurchaseOrderController extends BaseController {
    @Autowired
    private IPurchaseOrderService purchaseOrderService;


    /**
     * 获取采购订单
     * @param purchaseOrder
     * @return
     */
    @ApiOperationSupport(includeParameters = {"PurchaseOrder.purchaseOrderId", "PurchaseOrder.staffId", "PurchaseOrder.supplierInfoId", "PurchaseOrder.createTime"})
    @ApiOperation(value = "获取采购订单列表",notes = "查询所有采购订单信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PurchaseOrder purchaseOrder) {
        startPage();
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.selectPurchaseOrderList(purchaseOrder);
        return getDataTable(purchaseOrders);
    }

    /**
     * 导出采购订单
     *
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody PurchaseOrder purchaseOrder) {
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.selectPurchaseOrderList(purchaseOrder);
        ExcelUtil<PurchaseOrder> util = new ExcelUtil<PurchaseOrder>(PurchaseOrder.class);
        return util.exportExcel(purchaseOrders, "采购订单单数据");
    }

    /**
     * 新增采购订单
     * @param purchaseOrder 采购订单单据
     * @return
     */
    @ApiOperation(value = "新增采购订单", notes = "新增采购订单信息")
    @ApiImplicitParam(name = "purchaseOrder", value = "新增采购订单(参数：purchaseOrderId，staffId，supplierInfoId，requiredStorageTime，remark)", dataType = "PurchaseOrder")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PurchaseOrder purchaseOrder) {
        try {
            return toAjax(purchaseOrderService.insertPurchaseOrder(purchaseOrder));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新采购订单
     * @param purchaseOrder 采购订单单据
     * @return
     */
    @ApiOperation(value = "更新采购订单", notes = "更新采购订单")
    @ApiImplicitParam(name = "purchaseOrder", value = "更新采购订单", dataType = "PurchaseOrder")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody PurchaseOrder purchaseOrder) {
        try {
            purchaseOrderService.updatePurchaseOrder(purchaseOrder);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除采购订单
     * 仅修改删除标志位
     *
     * @param purchaseOrder 采购订单单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PurchaseOrder purchaseOrder) {

        return toAjax(purchaseOrderService.updatePurchaseOrderDeleteFlag(purchaseOrder));
    }

    /**
     * 新增采购订单
     * @param purchaseOrder 采购订单单据
     * @return
     */
    @PostMapping("/addByPurchaseApply")
    public AjaxResult addByPurchaseApply(@RequestBody PurchaseOrder purchaseOrder) {
        try {
            return toAjax( purchaseOrderService.insertPurchaseOrderByPurchaseApplyList(purchaseOrder));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 查询所有未入库的采购订单
     * @param purchaseOrder
     * @return
     */
    @ApiOperation(value = "查询所有未入库的采购订单",notes = "查询所有未入库的采购订单")
    @PostMapping("/allNotStoragePurchaseOrderList")
    public TableDataInfo selectAllNotStoragePurchaseOrderList(@RequestBody PurchaseOrder purchaseOrder) {
        startPage();
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.selectAllNotStoragePurchaseOrderList(purchaseOrder);
        return getDataTable(purchaseOrders);
    }

    /**
     * 查询某未入库采购订单的详情信息
     */
    @GetMapping(value = "/{purchaseOrderId}")
    @ApiOperation(value = "查询某未入库采购订单的详情信息",notes = "查询某未入库采购订单的详情信息")
    public AjaxResult selectNotStoragePurchaseOrderById(@PathVariable String purchaseOrderId)
    {
        return AjaxResult.success(purchaseOrderService.selectNotStoragePurchaseOrderById(purchaseOrderId));
    }

    /**
     * 查询某未质检的采购订单的详情信息
     */
    @GetMapping(value = "/inspect/{purchaseOrderId}")
    @ApiOperation(value = "查询某未质检的采购订单的详情信息",notes = "查询某未质检的采购订单的详情信息")
    public AjaxResult selectNotInspectPurchaseOrderById(@PathVariable String purchaseOrderId)
    {
        return AjaxResult.success(purchaseOrderService.selectNotInspectPurchaseOrderById(purchaseOrderId));
    }
}
